<template>
  <div class="play">
    <!-- 模糊背景(靠样式设置), && 有属性继续用, 固定定位 -->
    <div
      class="song-bg"
      :style="{
        'background-image': `url(${songInfo.al.picUrl})`
      }"
    ></div>
    <!-- 播放页头部导航 -->
    <div class="header">
      <van-icon
        name="arrow-left"
        size="20"
        class="left-incon"
        @click="$router.go(-1)"
      />
    </div>
    <!-- 播放页内容容器 -->
    <div class="song-wrapper" :class="{ play: isPlay }">
      <div class="song-turn ani">
        <div class="song-img">
          <!-- 封面 -->
          <img style="width: 100%" :src="songInfo.al.picUrl" alt="" />
        </div>
      </div>
      <!-- 播放盒子 -->
      <div class="start-box">
        <span class="song-start" @click="startClick"></span>
      </div>
      <!-- 歌曲信息 -->
      <div class="song-msg">
        <h2 class="m-song-h2">
          <span class="m-song-sname">{{ songInfo.name }}</span>
        </h2>
        <div class="arContent">
          <p class="ar">{{ songInfo.al.name }}</p>
        </div>
      </div>
      <div class="icon-item">
        <van-icon name="like-o" color="#de031e" />
        <van-icon name="down" color="#de031e" />
        <van-icon name="share-o" @click="showShare = true" color="#de031e" />
        <van-icon name="chat-o" badge="99+" @click="getComs" color="#de031e" />
        <van-icon name="thumb-circle-o" color="#de031e" />
      </div>
      <div class="huakuai">
        <van-slider v-model="startStep" :step="10" button-size="8px" />
      </div>
      <div class="musicIcon">
        <van-icon name="revoke" color="#fff" />
        <van-icon name="arrow-left" color="#fff" />
        <van-icon
          name="play-circle-o"
          size="40"
          class="bof"
          v-show="showPlay"
          @click="playClick"
          color="#fff"
        />
        <van-icon
          name="pause-circle-o"
          size="40"
          v-show="isIconShow"
          class="bof"
          color="#fff"
          @click="playClick"
        />
        <van-icon name="arrow" color="#fff" />
        <van-icon name="qr" color="#fff" @click="isClick" />
      </div>
      <!-- 操作杆 -->
      <div class="needle"></div>
    </div>
    <!-- 播放音乐真正的标签
      看接口文档: 音乐地址需要带id去获取(但是有的歌曲可能404)
     -->
    <audio
      ref="audio"
      preload="true"
      :src="
        `https://music.163.com/song/media/outer/url?id=${$route.query.id}.mp3`
      "
    ></audio>

    <van-share-sheet
      v-model="showShare"
      title="立即分享给好友"
      :options="options"
      @select="onSelect"
      class="share"
      z-index="999999"
    />

    <van-popup v-model="showPopup" closeable position="bottom" z-index="99999">
      <van-cell title="歌单列表"></van-cell>
      <van-cell
        :title="item"
        v-for="(item, index) in msgArr"
        :key="index"
        @click="isplayClick(index)"
      >
        <!-- 使用 right-icon 插槽来自定义右侧图标 -->
        <template>
          <van-icon name="play-circle-o" class="search-icon" />
        </template>
      </van-cell>
    </van-popup>
  </div>
</template>

<script>
import { song, songGeci } from '../find/find'
import { mapState } from 'vuex'

export default {
  name: 'play',
  data () {
    return {
      isPlay: false, // 控制音乐播放
      songInfo: {
        al: {}
      },
      geciList: '', // 歌词
      startStep: 0,
      showPlay: true, // 播放
      isIconShow: false, // 停止
      showShare: false,
      options: [
        { name: '微信', icon: 'wechat' },
        { name: '微博', icon: 'weibo' },
        { name: '复制链接', icon: 'link' },
        { name: '分享海报', icon: 'poster' },
        { name: '二维码', icon: 'qrcode' }
      ],
      showPopup: false,
      arrSong: [],
      msgArr: [],
      loading: false,
      finished: false
    }
  },
  methods: {
    startClick () {
      if (this.isPlay) {
        this.$refs.audio.pause()
        this.isPlay = false
      } else {
        this.$refs.audio.play()
        this.isPlay = true
      }
    },
    playClick () {
      if (this.showPlay) {
        this.$refs.audio.play()
        this.isPlay = true
        this.isIconShow = true
        this.showPlay = false
      } else {
        this.showPlay = true
        this.isIconShow = false
        this.$refs.audio.pause()
        this.isPlay = false
      }
    },
    async isClick () {
      this.showPopup = true
    },
    onSelect (option) {
      Toast(option.name)
      this.showShare = false
    },
    // 评论
    async getComs () {
      this.$store.commit('getCommId', this.$route.query.id)
      this.$router.push({ path: '/comment' })
    },
    isplayClick (index) {
      // console.log(this.$route.query.id)
      // console.log(this.$store.state.gedanSongId[index])
      this.$router.push({
        path: '/play',
        query: { id: this.$store.state.gedanSongId[index] }
      })
      this.startClick()
      this.showPopup = false
      this.playClick()
    }
  },

  // 页面进来渲染歌曲
  async created () {
    // if (this.isPlay) {
    //   this.$refs.audio.pause()
    //   this.isPlay = false
    // } else {
    //   this.$refs.audio.play()
    //   this.isPlay = true
    // }

    console.log(this.$route.query.id)
    let res = await song({ ids: this.$route.query.id })
    console.log(res)
    this.songInfo = res.data.songs[0]
    this.$store.commit('newSong', res.data.songs[0])

    let msg = await songGeci({ id: this.$route.query.id })
    console.log(msg)
    this.geciList = msg.data.lrc.lyric
    console.log(this.geciList)

    //歌单所有歌曲
    let arr = this.$store.state.gedanSongId
    // console.log(arr)
    arr.forEach(async v => {
      console.log(v)
      let res = await song({ ids: v })
      // console.log(res)
      for (let i = 0; i < arr.length; i++) {
        this.arrSong = res.data.songs[0].name
      }
      // console.log(this.arrSong)
      // console.log(this.arrSong)
      this.msgArr.push(this.arrSong)
      // console.log(this.masArr)
    })
  },
  computed: {
    ...mapState(['getGedanSongId'])
  }
}
</script>

<style scoped lang="less">
.header {
  height: 50px;
}
.play {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1000;
}
.song-bg {
  background-color: #161824;
  background-position: 50%;
  background-repeat: no-repeat;
  background-size: auto 100%;
  transform: scale(1.5);
  transform-origin: center;
  position: fixed;
  left: 0;
  right: 0;
  top: 0;
  height: 100%;
  overflow: hidden;
  z-index: 1;
  opacity: 1;
  filter: blur(25px);
}
.song-bg::before {
  content: ' ';
  background: rgba(0, 0, 0, 0.5);
  position: absolute;
  left: 0;
  top: 0;
  right: 0;
  bottom: 0;
}
.song-wrapper {
  position: fixed;
  width: 247px;
  height: 247px;
  left: 50%;
  top: 50px;
  transform: translateX(-50%);
  z-index: 10001;
  &.play {
    .needle {
      transform: rotate(-7deg);
    }
    .song-turn {
      animation-play-state: running;
    }
  }
}

.song-turn {
  width: 100%;
  height: 100%;
  background: url('../../assets/bg.png') no-repeat;
  background-size: 100%;
}
.start-box {
  position: absolute;
  width: 156px;
  height: 156px;
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  justify-content: center;
  align-items: center;
}
.song-start {
  width: 56px;
  height: 56px;
  background: url('../../assets/start.png');
  background-size: 100%;
}
.needle {
  position: absolute;
  transform-origin: left top;
  background: url('../../assets/needle-ab.png') no-repeat;
  background-size: contain;
  width: 73px;
  height: 118px;
  top: -40px;
  left: 112px;
  transition: all 0.6s;
  transform: rotate(-38deg);
}
.song-img {
  width: 154px;
  height: 154px;
  position: absolute;
  left: 50%;
  top: 50%;
  overflow: hidden;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}
.m-song-h2 {
  margin-top: 20px;
  text-align: center;
  font-size: 18px;
  color: #fefefe;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.arContent {
  margin-top: 20px;
}
.ar {
  font-size: 14px;
  color: #fff;
  text-align: center;
}
.left-incon {
  position: absolute;
  top: 10px;
  left: 10px;
  font-size: 24px;
  z-index: 10001;
  color: #fff;
}
.ani {
  animation: turn 5s linear infinite;
  animation-play-state: paused;
}
@keyframes turn {
  0% {
    -webkit-transform: rotate(0deg);
  }
  100% {
    -webkit-transform: rotate(360deg);
  }
}
.icon-item {
  display: flex;
}
.icon-item .van-icon {
  flex: 1;
}
.icon-item .van-info {
  position: absolute;
  top: 2px;
  right: 22px;
  color: #161824;
  border: 0;
  background-color: transparent;
}
.huakuai {
  margin-top: 50px;
}
.musicIcon {
  margin-top: 50px;
  display: flex;
  position: relative;
}
.musicIcon .van-icon {
  flex: 1;
}
.musicIcon .bof {
  display: inline-block;
  margin: -7px 13px 0 0;
}
.van-popup {
  height: 250px;
}
</style>
